/*
 *   This File is Part of CuteOgre Game Engine
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	Copyright 2010 Alexandre Pires ( c.alexandre.pires@gmail.com )
 *  Created on: 2010/09/04
 */

#include "Gui/Layout.h"
#include "Gui/StyleSheetManager.h"
#include "Gui/StyleAttribute.h"
#include "Gui/GUIOverlay.h"

#include "LogManager.h"
#include "ScriptManager.h"

#include "OGRE/OgreResourceManager.h"

#include <QDebug>

Layout::Layout ( const QString& rFile ) {

	mStyleSheet = 0;
	mXmlDocument = new QDomDocument ();

	LogManager::message ( QString ("Layout::Loading layout %1" ).arg ( rFile ) );

	QString content = readFileContents ( rFile );
	QString errorMsg;
	int errorLine, errorCol;

	mValid = mXmlDocument->setContent ( content, &errorMsg, &errorLine,
			&errorCol );

	if ( !mValid )
		LogManager::message (
				QString (
						"Layout::Error parsing XML Layout %1 at line %2, column %3\n Message: %4" ).arg (
						rFile ).arg ( errorLine ).arg ( errorCol ).arg (
						errorMsg ) );

	setupLayout ();
}

void Layout::render ( GUIOverlay* rLayout, const QPoint& rOffset, const QRegion& rRegion ) {

}

void Layout::draw () {

	if ( !isVisible() )
		return;

	render(this);
}

Layout::~Layout () {
	mStyleSheet = 0;
	delete mXmlDocument;
}

void Layout::show () {

	GUIOverlay::show();
	setVisibility( true );
}

void Layout::hide () {

	GUIOverlay::hide();
	setVisibility( false );
}

QString Layout::readFileContents ( const QString& rFile ) {

	Ogre::ResourceGroupManager* resourceManager =
			&Ogre::ResourceGroupManager::getSingleton ();

	if ( !resourceManager->resourceExists ( "Layouts", rFile.toStdString () ) ) {
		LogManager::message ( QString (
				"Layout::Cannot find %1 in gui directory" ).arg ( rFile ) );
		return QString::null;
	}

	Ogre::DataStreamPtr scriptStream = resourceManager->openResource (
			rFile.toStdString () );

	QString contents;

	while ( !scriptStream->eof () ) {

		QString line = QString ().fromStdString (
				scriptStream->getLine ( false ) );

		contents.append ( line + "\n" );
	}

	return contents;
}

void Layout::setupLayout () {

	QString name = mXmlDocument->documentElement ().attribute (
			"name", QString::null );

	if ( !name.isNull() ) {
		setName ( name );
	}

	QString stylesheetName = mXmlDocument->documentElement ().attribute (
			"stylesheet", QString::null );

	if ( !stylesheetName.isNull () ) {
		StyleSheetManager::loadStyleSheet ( stylesheetName );
		mStyleSheet = StyleSheetManager::styleSheet ( stylesheetName );
	}

	LogManager::message ( "Layout::Setting up script environment" );
	ScriptManager::setup( &mEngine );

	LogManager::message ( "Layout::Layout succefully loaded" );

	mStyle.readAttributtes ( mXmlDocument->documentElement() );

	QString styleName = mXmlDocument->documentElement ().attribute (
			"style", QString::null );

	mStyle = mStyleSheet->getStyle( "layout" );

	validateStyle();
}

void Layout::validateStyle () {

	StyleAttribute widthAttr = mStyle.getAttribute("width", StyleAttribute::null );
	StyleAttribute bgColorAttr = mStyle.getAttribute("background-color", StyleAttribute::null );
	StyleAttribute bgImageAttr = mStyle.getAttribute("background-image", StyleAttribute::null );

	int width = StyleAttribute::getInt( widthAttr );
	QColor bgColor = StyleAttribute::getColor( bgColorAttr );
	QStringList bgImage = StyleAttribute::getResource(bgImageAttr);

	qDebug() << width  ;
	qDebug() << bgColor.red() << bgColor.green() << bgColor.green() << bgColor.alpha()  ;

	for ( int i = 0; i < bgImage.size(); i++ )
		qDebug() << bgImage[i];
}
